﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SegGen
{
    class Current_Population_Pt:Population
    {

       


        public Current_Population_Pt(int numNucleotidesInCodon)
            : base(numNucleotidesInCodon)
        {
        }

        /// <summary>
        /// Fitness function for individuals from current population  is computed by 
        /// summing the hardness scores of individuals from external archive which dominates.
        /// </summary>
        /// <param name="_externalArchiveP"></param>
        public override void calculateFitness(Population _externalArchiveP)
        {
            // External archive P not = extArchP 
            double curPopPt_C = 0;
            double curPopPt_D = 0;
            double sumHardness = 0;

            // calculate Hardness for every individual in external archive P
            for (int ind1Index = 0; ind1Index < Individuals_List.Count; ind1Index++)
            {
                curPopPt_C = Individuals_List.ElementAt(ind1Index).C;
                curPopPt_D = Individuals_List.ElementAt(ind1Index).D;
                sumHardness = 0;
                //_currentPopulation.

                for (int ind2Index = 0; ind2Index < _externalArchiveP.Individuals_List.Count; ind2Index++)
                {
                    if (curPopPt_C <= _externalArchiveP.Individuals_List.ElementAt(ind2Index).C &&
                        curPopPt_D <= _externalArchiveP.Individuals_List.ElementAt(ind2Index).D)
                    {
                        sumHardness += _externalArchiveP.Individuals_List.ElementAt(ind2Index).H;
                    }

                }// end ind2 for

                // calculate fitness is inverse of its hardness value
                Individuals_List.ElementAt(ind1Index).F = (double) 1 / (1 + sumHardness);
            }// end ind1 for

            // after calculating of fitness we can calculate 
            // probability of selection
          //  this.calculateProbabilityOfSelection();

        }


        /// <summary>
        /// 
        /// </summary>
        public override void Update(Population tempPopulation)
        {
            double curPopP_C = 0;
            double curPopP_D = 0;


            List<Individual> tempIndList = tempPopulation.Individuals_List;
            foreach (Individual elenment in tempIndList)
            {
                if (Individuals_List.Contains(elenment) == false)
                    Individuals_List.Add(elenment);
            }
           
            /// calculate fitness for Current Population
            for (int curPopIndex = 0; curPopIndex < Individuals_List.Count; curPopIndex++)
            {
                curPopP_C = Individuals_List.ElementAt(curPopIndex).C;
                curPopP_D = Individuals_List.ElementAt(curPopIndex).D;


                for (int extArchiveIndex = 0; extArchiveIndex < NeighbourPopulation.Individuals_List.Count && Individuals_List.Count > 0; extArchiveIndex++)
                {
                    if (curPopP_C < NeighbourPopulation.Individuals_List.ElementAt(extArchiveIndex).C &&
                        curPopP_D <= NeighbourPopulation.Individuals_List.ElementAt(extArchiveIndex).D ||
                        curPopP_C <= NeighbourPopulation.Individuals_List.ElementAt(extArchiveIndex).C &&
                        curPopP_D < NeighbourPopulation.Individuals_List.ElementAt(extArchiveIndex).D)
                    {
                        
                        
                        //if (NeighbourPopulation.Individuals_List.Contains(Individuals_List.ElementAt(curPopIndex)) == true)
                        //{
                        //    NeighbourPopulation.Individuals_List.Remove(Individuals_List.ElementAt(curPopIndex));
                            
                        //}

                    //}
                    //else
                    //{
                        if (NeighbourPopulation.Individuals_List.Contains(Individuals_List.ElementAt(curPopIndex)) == false)
                        {
                            NeighbourPopulation.Individuals_List.Add(Individuals_List.ElementAt(curPopIndex));
                            
                        }

                        Individuals_List.Remove(Individuals_List.ElementAt(curPopIndex));
                        curPopIndex--;
                        break;
                    }

                }// end for
               
            }// for

          




           
        }

        public override Individual ExtractionOfSolution()
        {
            double maxAgg = 0;
            int maxAggIndex = -1;

            // go to all individuals and find 
            // the best individual according to aggregation 
            for (int i = 0; i < Individuals_List.Count; i++)
            {
                if (Individuals_List.ElementAt(i).Agg > maxAgg)
                {
                    maxAgg = Individuals_List.ElementAt(i).Agg;
                    maxAggIndex = i;
                }
            }

            if (maxAggIndex == -1) return null;

            return Individuals_List.ElementAt(maxAggIndex);
        }
    }
}
